﻿// JSON C# Class Generator
// http://at-my-window.blogspot.com/?page=json-class-generator

using System;
using Newtonsoft.Json.Linq;
using JsonCSharpClassGenerator;
using TomatoSynchronizer.Store;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;

namespace TomatoSynchronizer.Entities
{
	public class Category : IComparable
	{
		private CategoryStore store;

		public CategoryStore Store {
			get { return store; }
			set { store = value; }
		}

		public override int GetHashCode()
		{
			return base.GetHashCode();
		}

		public Category(string json) : this(JObject.Parse(json))
		{
		}

		private JObject __jobject;
		public Category(JObject obj)
		{
			this.__jobject = obj;
		}

		public Category()
		{
		}

		public int CompareTo(object obj)
		{

			if (obj is Category) {

				Category c = (Category)obj;

				DateTime dateModifiedSrc = DateTime.Parse(_last_modified);
				DateTime dateModifiedDest = DateTime.Parse(c.last_modified);

				return dateModifiedSrc.CompareTo(dateModifiedDest);

			} else
				throw new ArgumentException("Object is not a Category");
		}

		public override bool Equals(object obj)
		{
			if (obj is Category) {
				Category category = (Category)obj;
				if (category.categories_image == _categories_image && category.categories_name == _categories_name && category.categories_page_title == _page_title && category.categories_status == _categories_status && category._categories_url == _categories_url && category._categories_meta_description == _categories_meta_description && category._categories_meta_keywords == _categories_meta_keywords) {
					return true;
				}
			}

			return false;
		}

		private string _categories_id;
		public string categories_id {
			get {
				if (_categories_id == null) {
					_categories_id = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject, "categories_id"));
				}
				return _categories_id;
			}
			set { _categories_id = value; }
		}

		private string _categories_image;
		public string categories_image {
			get {
				if (string.IsNullOrEmpty(_categories_image) && __jobject != null) {
					_categories_image = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject, "categories_image"));
				}
				return _categories_image;
			}
			set { _categories_image = value; }
		}

		private string _parent_id;
		public string parent_id {
			get {
				if (_parent_id == null) {
					_parent_id = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject, "parent_id"));
				}
				return _parent_id;
			}
			set { _parent_id = value; }
		}

		private string _sort_order;
		public string sort_order {
			get {
				if (_sort_order == null) {
					_sort_order = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject, "sort_order"));
				}
				return _sort_order;
			}
			set { _sort_order = value; }
		}

		private string _categories_status;
		public string categories_status {
			get {
				if (_categories_status == null) {
					_categories_status = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject, "categories_status"));
				}
				return _categories_status;
			}
			set { _categories_status = value; }
		}

		private string _date_added;
		public string date_added {
			get {
				if (_date_added == null) {
					_date_added = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject, "date_added"));

					//cannot be null
					if (_date_added == null) {
						throw new Exception("date_added cannot be null");
					}
				}
				return _date_added;
			}
			set { _date_added = value; }
		}

		private string _last_modified;
		public string last_modified {
			get {
				if (_last_modified == null) {
					_last_modified = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject, "last_modified"));

					//i don't want to return a null date
					if (_last_modified == null) {
						return DateTime.MinValue.ToString();
					}
				}
				return _last_modified;
			}
			set { _last_modified = value; }
		}

		private string _language_id;
		public string language_id {
			get {
				if (_language_id == null) {
					_language_id = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject, "language_id"));
				}
				return _language_id;
			}
			set { _language_id = value; }
		}

		private string _categories_name;
		public string categories_name {
			get {
				if (_categories_name == null) {
					_categories_name = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject, "categories_name"));
				}
				return _categories_name;
			}
			set { _categories_name = value; }
		}
		
		private Dictionary<string, string> _categories_names;
		public Dictionary<string, string> categories_names
		{
			get
			{
				if(_categories_names == null)
				{
					_categories_names = new Dictionary<string, string>();
					
					if(__jobject != null)
					{
						if(store != null)
						{
							var languages = store.site.GetLanguageStore().LoadItems();
							
							foreach(Language language in languages)
							{
								_categories_names["categories_name[" + language.languages_id + "]"] = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject,"categories_name[" + language.languages_id + "]"));
							}
						}
					}
				}
				
				return _categories_names;
			}
			set
			{
				_categories_names = value;
			}
		}

		private string _categories_url;
		public string categories_url {
			get {
				if (string.IsNullOrEmpty(_categories_url) && __jobject != null) {
					_categories_url = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject, "categories_url"));
				}
				return _categories_url;
			}
			set { _categories_url = value; }
		}
		
		private Dictionary<string, string> _categories_urls;
		public Dictionary<string, string> categories_urls
		{
			get
			{
				if(_categories_urls == null)
				{
					_categories_urls = new Dictionary<string, string>();
					
					if(__jobject != null)
					{
						if(store != null)
						{
							var languages = store.site.GetLanguageStore().LoadItems();
							
							foreach(Language language in languages)
							{
								_categories_urls["categories_ur[" + language.languages_id + "]"] = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject,"categories_ur[" + language.languages_id + "]"));
							}
						}
					}
				}
				
				return _categories_urls;
			}
			set
			{
				_categories_urls = value;
			}
		}

		private string _categories_page_title;
		public string categories_page_title {
			get {
				if (_categories_page_title == null) {
					_categories_page_title = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject, "categories_page_title"));
				}
				return _categories_page_title;
			}
			set { _categories_page_title = value; }
		}	
		
		private string _categories_meta_keywords;
		public string categories_meta_keywords {
			get {
				if (_categories_meta_keywords == null) {
					_categories_meta_keywords = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject, "categories_meta_keywords"));
				}
				return _categories_meta_keywords;
			}
			set { _categories_meta_keywords = value; }
		}				

		private string _categories_meta_description;
		public string categories_meta_description {
			get {
				if (_categories_meta_description == null) {
					_categories_meta_description = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject, "categories_meta_description"));
				}
				return _categories_meta_description;
			}
			set { _categories_meta_description = value; }
		}				

		private int _childs_count;
		public int childs_count {
			get {
				if (_childs_count == null) {
					_childs_count = JsonClassHelper.ReadInteger(JsonClassHelper.GetJToken<JValue>(__jobject, "childs_count"));
				}
				return _childs_count;
			}
			set { _childs_count = value; }
		}

		private int _products_count;
		public int products_count {
			get {
				if (_products_count == null) {
					_products_count = JsonClassHelper.ReadInteger(JsonClassHelper.GetJToken<JValue>(__jobject, "products_count"));
				}
				return _products_count;
			}
			set { _products_count = value; }
		}

		private string _parent_category_id;
		public string parent_category_id {
			get {
				if (_parent_category_id == null) {
					_parent_category_id = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject, "parent_category_id"));
				}
				return _parent_category_id;
			}
			set { _parent_category_id = value; }
		}

		[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
		private object[] _ratings;
		public object[] ratings {
//                if(_ratings == null)
//                    _ratings = (object[])JsonClassHelper.ReadArray<object>(JsonClassHelper.GetJToken<JArray>(__jobject, "ratings"), JsonClassHelper.ReadObject, typeof(object[]));
			get { return _ratings; }
			set { _ratings = value; }
		}

		private string _page_title;
		public string page_title {
			get {
				if (language_id != null) {
					if (_page_title == null) {
						_page_title = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject, "page_title[" + _language_id + "]"));
					}
					return _page_title;
				}
				throw new Exception("Category language not set");
			}
			set { _page_title = value; }
		}
		
		private Dictionary<string, string> _page_titles;
		public Dictionary<string, string> page_titles
		{
			get
			{
				if(_page_titles == null)
				{
					_page_titles = new Dictionary<string, string>();
					
					if(__jobject != null)
					{
						if(store != null)
						{
							var languages = store.site.GetLanguageStore().LoadItems();
							
							foreach(Language language in languages)
							{
								_page_titles["page_title[" + language.languages_id + "]"] = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject,"page_title[" + language.languages_id + "]"));
							}
						}
					}
				}
				
				return _page_titles;
			}
			set
			{
				_page_titles = value;
			}
		}

		private string _meta_keywords;
		public string meta_keywords {
			get {
				if (language_id != null) {
					if (_meta_keywords == null) {
						_meta_keywords = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject, "meta_keywords[" + _language_id + "]"));
					}
					return _meta_keywords;
				}
				throw new Exception("Category language not set");
			}
			set { _meta_keywords = value; }
		}
		
		private Dictionary<string, string> _meta_keywordss;
		public Dictionary<string, string> meta_keywordss
		{
			get
			{
				if(_meta_keywordss == null)
				{
					_meta_keywordss = new Dictionary<string, string>();
					
					if(__jobject != null)
					{
						if(store != null)
						{
							var languages = store.site.GetLanguageStore().LoadItems();
							
							foreach(Language language in languages)
							{
								_meta_keywordss["meta_keywords[" + language.languages_id + "]"] = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject,"meta_keywords[" + language.languages_id + "]"));
							}
						}
					}
				}
				
				return _meta_keywordss;
			}
			set
			{
				_meta_keywordss = value;
			}
		}

		private string _meta_description;
		public string meta_description {
			get {
				if (language_id != null) {
					if (_meta_description == null) {
						_meta_description = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject, "meta_description[" + _language_id + "]"));
					}
					return _meta_description;
				}
				throw new Exception("Category language not set");
			}
			set { _meta_description = value; }
		}
		
		private Dictionary<string, string> _meta_descriptions;
		public Dictionary<string, string> meta_descriptions
		{
			get
			{
				if(_meta_descriptions == null)
				{
					_meta_descriptions = new Dictionary<string, string>();
					
					if(__jobject != null)
					{
						if(store != null)
						{
							var languages = store.site.GetLanguageStore().LoadItems();
							
							foreach(Language language in languages)
							{
								_meta_descriptions["meta_description[" + language.languages_id + "]"] = JsonClassHelper.ReadString(JsonClassHelper.GetJToken<JValue>(__jobject,"meta_description[" + language.languages_id + "]"));
							}
						}
					}
				}
				
				return _meta_descriptions;
			}
			set
			{
				_meta_descriptions = value;
			}
		}
	}
}
